package Server;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import Classes.Auth;
import Classes.Book;
import Constantes.CONSTANTES;
import Interfaces.iLibrary;

public class LibraryServer extends UnicastRemoteObject implements iLibrary{
	private static final long serialVersionUID = 1L;
	private ArrayList<Book> Books = null;
	private String UserLogged = null;
	private boolean isAdmin;
	
	protected LibraryServer() throws RemoteException {
		if ( Books == null ){
			Books = new ArrayList<Book>();
		}
		this.UserLogged = new String();
		this.isAdmin = false;
		
		System.out.println("Server Successfully Created...");
	}

	public boolean loginAdmin ( Auth aut ) throws RemoteException {
		if ( aut.getName().equals(CONSTANTES.Name) ){
			if ( aut.getPassword().equals(CONSTANTES.Password) ){
				this.UserLogged = CONSTANTES.Name;
				this.isAdmin = true;
				System.out.println("The Admin User is now logged in.");
				return true;
			} else {
				System.out.println("Someone tryied to log in using Admin Account, but he wasn't succeded!");
				return false;
			}
		} else {
			System.out.println("Someone tryied to log in using Admin Account, but he wasn't succeded!");
			return false;
		}
	}
	
	public boolean loginUser( Auth aut ) throws RemoteException {
		if ( aut.getName() != null ){
			this.UserLogged = aut.getName();
			this.isAdmin = false;
			System.out.println("The user [ " + aut.getName() + " ] is now logged in.");
			return true;
		}else
			return false;
	}
	
	public boolean addBook(Book B) throws RemoteException {
		if ( isAdmin )
			if ( Books.add(B) ){
				System.out.println("You added the following book:");
				B.printBook();
				return true;
			} else {
				return false;	
			}
		else
			return false;
	}

	
	public boolean borrowBook(Book B) throws RemoteException {
		if ( !isAdmin ){
			for (Book C : Books) {
				if ( C.equals(B) ){
					if ( C.getUser() == null ){
						C.setUser(this.UserLogged);
						return true;
					} else {
						return false;
					}
				}
			}
		}
		return false;
	}

	
	public ArrayList<Book> listOfBooks() throws RemoteException {
		return this.Books;
	}

	public boolean removeBook(Book B) throws RemoteException {
		if ( isAdmin ){
			for (Book C : Books) {
				if ( C.equals(B) )
					return Books.remove(C);
			}
		}
		return false;
	}

	public boolean returnBook(Book B) throws RemoteException {
		if ( !isAdmin ){
			for (Book C : Books) {
				if ( C.equals(B) ){
					if(this.UserLogged == C.getUser()){
						C.setUser(null);
						return true;
					}
					else{
						return false;
					}
				}
			}
		}
		return false;
	}
	
	public static void main(String[] args) {
		try
		{
			LibraryServer server = new LibraryServer( );
			LocateRegistry.createRegistry( CONSTANTES.Port );
			String service = CONSTANTES.ServerLocal + CONSTANTES.Port + CONSTANTES.AppName;
//			String service = CONSTANTES.ServerLocal + CONSTANTES.Port + CONSTANTES.AppName;
			Naming.bind( service , server );
		}
		catch ( Exception e )
		{
			JOptionPane.showMessageDialog(null, "Probably there is already a server running or the port is busy!");
			System.exit(-1);
			//System.out.println ( e );
		}
	}
}
